function CICR_SensitivityAnalysis()
% This function shows the sensitivity of [Ca]i to different parameters ('paramNoSet') in
% the model when they change from 'minPercent' to 'maxPercent' of their
% original value. For increasing the resolution, one should increase the
% variable 'nPoints' . For adding or removing different parameter, one
% should add or remove index of those parameters to the list 'paramNoSet'.
clc
close all
[originalCONSTANTS IC] = CICR_Constants();
CONSTANTS = originalCONSTANTS;
% Parametes of interest : SR volume, PMCA density, i_pCa_max, G_CaB,
simulation_duration = 1e5; % debug
nPoints = 3; % change this parameter for controling the resolution
minPercent = 0.5; %
maxPercent = 1.5;%
paramValues = zeros( length(CONSTANTS), nPoints +1);
basal = zeros( length(CONSTANTS), nPoints +1);
peak = zeros( length(CONSTANTS), nPoints +1);

global paramNoSet
paramNoSet = [4 6] ; % diferenet parameter list
for paramNo= paramNoSet
CONSTANTS = originalCONSTANTS;
step = (maxPercent* CONSTANTS(paramNo)- minPercent* CONSTANTS(paramNo))/ nPoints;
stepNo = 0;
for value = minPercent * CONSTANTS(paramNo): step: maxPercent * CONSTANTS(paramNo)
stepNo = stepNo + 1;
CONSTANTS(paramNo) = value;
paramValues(paramNo, stepNo) = value;
state = CICR_CompCore(CONSTANTS, IC, simulation_duration,1);
[b p] = getFeatures(state(:,14));
basal(paramNo, stepNo) = b;
peak(paramNo, stepNo) = p;


end
end
handles = visualizeDate(paramValues,basal,peak);
SaveFigures(handles);
end
function SaveFigures(handles)

% Read the time
experiment_clock = int2str(fix(clock));

% Make a new directory and put the current time as its name
mkdir('/home/darwin/Projects/simple CICR/results/Sensitivity Analysis Results', experiment_clock);

% Make a path for jpg files which are about to be created
experiment_path = strcat('/home/darwin/Projects/simple CICR/results/Sensitivity Analysis Results','/',experiment_clock);

% Save the figurse as jpg files in the specified path
for counter = 1:length(handles)
saveas(handles(counter), strcat(experiment_path,'/',strcat(int2str(counter),'.jpg')));
end

end
function handles= visualizeDate(paramValues, basalVec, peakVec)
global paramNoSet
% handles = figure.empty(paramNoSet,0);
% handles = [];
counter = 0;
for paramNo = paramNoSet
counter = counter + 1;
handles(counter) = figure(); % debug
plot(paramValues(paramNo,:), peakVec(paramNo,:),'-b*',...
paramValues(paramNo,:), basalVec(paramNo,:),'-r^')
legend('peak value', 'basal level')
ylabel('[Ca]_i \mu M');
end
end

function [basal peak]= getFeatures(signal)
basal = signal(round(length(signal)/3));
peak = max(signal(:));
end
